//
//  UserinfoViewController.m
//  F50
//
//  Created by zack on 15/5/17.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "UserinfoViewController.h"
#import "UserInfoTableViewCell.h"
#import "ContentViewController.h"
@interface UserinfoViewController ()
{
    UITableView *_tableView;
}
@end

@implementation UserinfoViewController
- (void)createBackground
{
    UIView *nav_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    nav_bg.backgroundColor = RGB(59, 107, 178);
    [self.view addSubview:nav_bg];
    
    
    //中间标题
    UILabel *  nav_title=[[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 80, 20, 160, 40)];
    nav_title.text=@"个人信息";
    nav_title.textAlignment = NSTextAlignmentCenter;
    nav_title.font=[UIFont boldSystemFontOfSize:20];
    nav_title.textColor=[UIColor whiteColor];
    nav_title.backgroundColor=[UIColor clearColor];
    [nav_bg addSubview:nav_title];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(20, 32, 15, 18);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [nav_bg addSubview:backBtn];
    
    UIView *heard = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 120)];
    heard.backgroundColor = [UIColor clearColor];
    [self.view addSubview:heard];
    
    self.photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2 - 40, 20, 80, 80)];
    [self.photoImage sd_setImageWithURL:[USER_DEFAULT objectForKey:@"avatar"] placeholderImage:[UIImage imageNamed:@"profile"]];
    self.photoImage.layer.cornerRadius = 40;
    self.photoImage.layer.masksToBounds = YES;
    [heard addSubview:self.photoImage];
    
    UIImageView *goImage = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 30, 45, 20*imageW, 20*imageW)];
    goImage.image = [UIImage imageNamed:@"go"];
    [heard addSubview:goImage];
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 45, 50, 20)];
    lab.text = @"头像";
    lab.textColor = [UIColor whiteColor];
    lab.font = [UIFont systemFontOfSize:17];
    [heard addSubview:lab];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)] ;
    tap.delegate = self;
    [heard addGestureRecognizer:tap];
  
    
}
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createBackground];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 170, WIDTH,200) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled =NO; //设置tableview 不能滚动
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    UserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[UserInfoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
    switch (indexPath.section) {
        case 0:
            cell.chanceLab.text = @"一句话介绍";
            cell.infoLab.text = [USER_DEFAULT objectForKey:@"content"];
;
            break;
        case 1:
            cell.chanceLab.text = @"手机号";
         cell.infoLab.text = [USER_DEFAULT objectForKey:@"phone"];
            cell.view.hidden = YES;
            break;
      
            
        default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            ContentViewController *content = [[ContentViewController alloc]init];
            content.succeedBlock = ^(NSString *str){
//                NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
                UserInfoTableViewCell *cell = (UserInfoTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
                if (indexPath.section == 0) {
                    cell.infoLab.text = str;
                }
            };
            [self.navigationController pushViewController:content animated:YES];
        }
            break;
            
        default:
            break;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    view.backgroundColor = [UIColor clearColor];
    return  view;
}

- (void)handleTap {
    UIActionSheet *photoBtnActionSheet =
    [[UIActionSheet alloc] initWithTitle:nil
                                delegate:self
                       cancelButtonTitle:@"取消"
                  destructiveButtonTitle:nil
                       otherButtonTitles:@"从相册选择",@"拍照", nil];
    [photoBtnActionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];
    [photoBtnActionSheet showInView:[self.view window]];

}

#pragma mark - - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    
    NSLog(@"选择相机还是照片库%ld",(long)buttonIndex);
    if (buttonIndex == 0) {
        //Show Photo Library
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        {
            UIImagePickerController *imgPickerVC = [[UIImagePickerController alloc] init];
            [imgPickerVC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [imgPickerVC.navigationBar setBarStyle:UIBarStyleBlack];
            imgPickerVC.delegate = self;
            //            if (pick_flag)
            //            {
            //                [imgPickerVC setAllowsEditing:YES];
            //            }
            
            //显示Image Picker
            [self presentViewController:imgPickerVC animated:YES completion:^{
                NSLog(@"123435453");
            }];
            
            //                [self presentModalViewController:imgPickerVC animated:YES];
            //                [imgPickerVC release];
        }else {
            NSLog(@"Album is not available.");
        }
    }
    if (buttonIndex == 1) {
        //Take Photo with Camera
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *cameraVC = [[UIImagePickerController alloc] init];
            [cameraVC setSourceType:UIImagePickerControllerSourceTypeCamera];
            [cameraVC.navigationBar setBarStyle:UIBarStyleBlack];
            [cameraVC setDelegate:self];
            //            if (pick_flag)
            //            {
            //                [cameraVC setAllowsEditing:YES];
            //            }
            //显示Camera VC
            [self presentViewController:cameraVC animated:YES completion:^{
                NSLog(@"拍照");
            }];
            
            //                [self presentModalViewController:cameraVC animated:YES];
            //                [cameraVC release];
        }
    }
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"%@",info);
   
   UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.photoImage.image = image;
     NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"token"],@"token",nil];
    [[AFCustomObject shareAFCustomObject] uploadImgWithParams:date withUploadImage:image success:^(NSDictionary *result) {
        NSLog(@"1111111111");
        NSLog(@"%@",result);
        [USER_DEFAULT setObject:[result objectForKey:@"newicon"] forKey:@"avatar"];
        [USER_DEFAULT synchronize];
    } fail:^{
        NSLog(@"1111111111");

    }];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
